package org.theonept.travian.t4a.message;

import org.theonept.travian.model.Compte;
import org.theonept.travian.model.Message;
import org.theonept.travian.t4a.R;
import org.theonept.travian.t4a.message.WriteMessage.MessageWriteAsync;
import org.theonept.travian.util.AsyncWeb;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ReadMessageActivity extends Activity {
	private TextView fromText;
	private TextView subjectText;
	private TextView messageText;
	private Message message;
	private EditText messageReply;
	private Compte compte;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_read_message);
		fromText = (TextView) findViewById(R.id.textFrom);
		subjectText = (TextView) findViewById(R.id.textSubject);
		messageText = (TextView) findViewById(R.id.textMessage);
		message = (Message) getIntent().getExtras().getSerializable("message");
		compte = (Compte) getIntent().getExtras().getSerializable("compte");
		fromText.setText(Html.fromHtml(message.getExpediteur()));
		subjectText.setText(Html.fromHtml(message.getSujet()));
		if (message.getMessage() != null) {
			messageText.setText(Html.fromHtml(message.getMessage()));
		} else {
			setProgressBarIndeterminateVisibility(true);
			new MessageReadAsync(compte).execute(compte);
		}

		messageReply = (EditText) findViewById(R.id.replyMessage);

		// Show the Up button in the action bar.
		setupActionBar();
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.read_message, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case R.id.action_send_message:
        	new MessageReplyAsync(compte).execute(compte);
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			fin();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void fin() {
		setResult(RESULT_OK, getIntent());
		finish();
	}



	class MessageReplyAsync extends AsyncWeb<Compte, Void, Boolean> {

		public MessageReplyAsync(Compte compte) {
			super(compte);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			fin();
			super.onPostExecute(result);
		}

		@Override
		protected Boolean executeWebTask(Compte... params) {
			message.reply(messageReply.getText().toString());
			return true;
		}
	}

	class MessageReadAsync extends AsyncWeb<Compte, Void, Boolean> {

		public MessageReadAsync(Compte compte) {
			super(compte);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Boolean result) {

			super.onPostExecute(result);
			if (message.getMessage() != null) {
				messageText.setText(Html.fromHtml(message.getMessage()));
			}
			setProgressBarIndeterminateVisibility(false);
		}

		@Override
		protected Boolean executeWebTask(Compte... params) {
			message.getContent();
			return true;
		}
	}
}
